﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Korhaz
{
    public partial class Elojegyzes : Form
    {
        Betegek b;
        Elojegyzesek elo;
        Orvosok o;
        Asszisztensek a;
        Mutok m;
        Kezelesek k;

        private bool Err;
        private string ErrMsg;
        public Elojegyzes()
        {
            InitializeComponent();
            Err = false;
            b = new Betegek(ref Err);
            if (Err)
            {
                MessageBox.Show("Az objektumot nem lehet letrehozni");
            };
            elo = new Elojegyzesek(ref Err);
            if (Err)
            {
                MessageBox.Show("Az objektumot nem lehet letrehozni");
            };
        }

        private void butKilep_Click(object sender, EventArgs e)
        {
            Belepes login = new Belepes();
            login.Show();
            this.Hide();
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            Beteg beteg = new Beteg();
            beteg.Show();
            this.Hide();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Betoltes()
        {
            string ErrMsg = "";
            SqlDataReader reader = b.BetegLista(ref ErrMsg);
                listView1.Items.Clear();
                if (ErrMsg == "OK")
                {
                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem(reader[0].ToString());
                        item.SubItems.Add(reader[1].ToString());
                        item.SubItems.Add(reader[2].ToString());
                        item.SubItems.Add(reader[3].ToString());
                        item.SubItems.Add(reader[4].ToString());
                        listView1.Items.Add(item);
                    }
                    reader.Close();
                }
                else
                {
                    {
                        this.Close();
                    }
                }

                ErrMsg = "";
                reader = elo.ElojegyzesLista(ref ErrMsg);
                listView2.Items.Clear();

                if (ErrMsg == "OK")
                {
                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem(reader[0].ToString());
                        item.SubItems.Add(reader[1].ToString());
                        item.SubItems.Add(reader[2].ToString());
                        item.SubItems.Add(reader[3].ToString());
                        item.SubItems.Add(reader[4].ToString());
                        item.SubItems.Add(reader[5].ToString());
                        item.SubItems.Add(reader[6].ToString());
                        listView2.Items.Add(item);
                    }
                    reader.Close();
                }
                else
                {
                    {
                        this.Close();
                    }
                }
                ErrMsg = "";
                reader = o.OrvosLista1(ref ErrMsg);
                listView3.Items.Clear();

                if (ErrMsg == "OK")
                {
                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem(reader[0].ToString());
                        item.SubItems.Add(reader[1].ToString());
                        item.SubItems.Add(reader[2].ToString());
                        item.SubItems.Add(reader[3].ToString());
                        item.SubItems.Add(reader[4].ToString());
                        item.SubItems.Add(reader[5].ToString());
                        listView3.Items.Add(item);
                    }
                    reader.Close();
                }
                else
                {
                    {
                        this.Close();
                    }
                }
                ErrMsg = "";
                reader = a.AsszisztensLista1(ref ErrMsg);
                listView4.Items.Clear();

                if (ErrMsg == "OK")
                {
                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem(reader[0].ToString());
                        item.SubItems.Add(reader[1].ToString());
                        listView4.Items.Add(item);
                    }
                    reader.Close();
                }
                else
                {
                    {
                        this.Close();
                    }
                }
                ErrMsg = "";
                reader = b.BetegLista(ref ErrMsg);
                if (ErrMsg == "OK")
                {
                    while (reader.Read())
                    {
                        comboBetegID.Items.Add(reader[0]);
                    }
                    reader.Close();
                }
                else
                {
                    {
                        this.Close();
                    }
                }
                ErrMsg = "";
                o = new Orvosok(ref Err);
                reader = o.OrvosLista(ref ErrMsg);
                if (ErrMsg == "OK")
                {
                    while (reader.Read())
                    {
                        comboOrvosID.Items.Add(reader[0]);
                    }
                    reader.Close();
                }
                else
                {
                    {
                        this.Close();
                    }
                }

                ErrMsg = "";
                //reader.Close();
                a = new Asszisztensek(ref Err);
                reader = a.AsszisztensLista(ref ErrMsg);

                if (ErrMsg == "OK")
                {
                    while (reader.Read())
                    {
                        comboAsszID.Items.Add(reader[0]);
                    }
                    reader.Close();
                }
                else
                {
                    {
                        this.Close();
                    }
                }
                ErrMsg = "";
                //reader.Close();
                m = new Mutok(ref Err);
                reader = m.MutoLista(ref ErrMsg);

                if (ErrMsg == "OK")
                {
                    while (reader.Read())
                    {
                        comboMuto.Items.Add(reader[0]);
                    }
                    reader.Close();
                }
                else
                {
                    {
                        this.Close();
                    }
                }
        }

        private void Betoltes2()
        {
            ErrMsg = "";
               SqlDataReader reader = elo.ElojegyzesLista(ref ErrMsg);
                listView2.Items.Clear();

                if (ErrMsg == "OK")
                {
                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem(reader[0].ToString());
                        item.SubItems.Add(reader[1].ToString());
                        item.SubItems.Add(reader[2].ToString());
                        item.SubItems.Add(reader[3].ToString());
                        item.SubItems.Add(reader[4].ToString());
                        item.SubItems.Add(reader[5].ToString());
                        item.SubItems.Add(reader[6].ToString());
                        listView2.Items.Add(item);
                    }
                    reader.Close();
                }
                else
                {
                    {
                        this.Close();
                    }
                }
        }

        private void Elojegyzes_Load(object sender, EventArgs e)
        {
            txtEloID.Enabled = false;
            dateDatum.Enabled = false;
            dateOra.Enabled = false;
            comboAsszID.Enabled = false;
            comboBetegID.Enabled = false;
            comboMuto.Enabled = false;
            comboOrvosID.Enabled = false;
           // txtBetegID.Enabled = false;

            if (ErrMsg == "OK")
            {
                MessageBox.Show("Error");
            }
            else
            {
                b = new Betegek(ref Err);
                elo = new Elojegyzesek(ref Err);
                o = new Orvosok(ref Err);
                a = new Asszisztensek(ref Err);
                this.Betoltes();
            }
            //txtBetegID.Enabled = false;
            //txtKezelesID1.Enabled = false;
            
            //txtElojegyzes.Enabled = false;
            //txtMutoID.Enabled = false;
        }

       

        
        private void butUj_Click_1(object sender, EventArgs e)
        {
            checkBox1.Checked = true;
            int imaxkod = elo.MaxKod(ref ErrMsg);
            txtEloID.Text = Convert.ToString(imaxkod + 1);
            txtEloID.Enabled = false;
            dateDatum.Enabled = true;
            dateOra.Enabled = true;
            comboOrvosID.Enabled = true;
            comboMuto.Enabled = true;
            comboBetegID.Enabled = true;
            comboAsszID.Enabled = true;
        }

        
        private void butTorol_Click(object sender, EventArgs e)
        {
           
        }

        

        private void butMegse_Click(object sender, EventArgs e)
        {
            txtEloID.Clear();
        }

        private void pictureBox1_Click_1(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void pictureBox3_Click_1(object sender, EventArgs e)
        {
            Beteg b = new Beteg();
            b.Show();
            this.Hide();
        }

        private void butUj_Click(object sender, EventArgs e)
        {
            checkBox1.Checked = true;
            checkBox1.Enabled = false;
            int imaxkod = elo.MaxKod(ref ErrMsg);
            txtEloID.Text = Convert.ToString(imaxkod + 1);
            txtEloID.Enabled = false;
            comboAsszID.Enabled = true;
            comboBetegID.Enabled = true;
            comboMuto.Enabled = true;
            comboOrvosID.Enabled = true;
            dateDatum.Enabled = true;
            dateOra.Enabled = true;
        }

        private void butMod_Click(object sender, EventArgs e)
        {
            checkBox1.Checked = false;
            checkBox1.Enabled = false;
            txtEloID.Enabled = false;
            dateDatum.Enabled = true;
            dateOra.Enabled = true;
            comboAsszID.Enabled = true;
            comboBetegID.Enabled = true;
            comboMuto.Enabled = true;
            comboOrvosID.Enabled = true;

            ListViewItem newItem = new ListViewItem();
            newItem = listView2.SelectedItems[0];
            txtEloID.Text = newItem.SubItems[0].Text;
            dateDatum.Text = newItem.SubItems[1].Text;
            dateOra.Text = newItem.SubItems[2].Text;
            comboBetegID.Text = newItem.SubItems[3].Text;
            comboOrvosID.Text = newItem.SubItems[4].Text;
            comboAsszID.Text = newItem.SubItems[5].Text;
            comboMuto.Text = newItem.SubItems[6].Text;
        }

        private void txtFilterOrvos_TextChanged(object sender, EventArgs e)
        {
            if (txtFilterOrvos.Text != "")
            {
                listView3.Items.Clear();
                string ErrMsg = "";
                String fL = txtFilterOrvos.Text[0].ToString();
                SqlDataReader reader = o.ElsoOrvosLista(ref ErrMsg, fL);
                if (ErrMsg == "OK")
                {
                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem(reader[0].ToString());
                        item.SubItems.Add(reader[1].ToString());
                        item.SubItems.Add(reader[2].ToString());
                        item.SubItems.Add(reader[3].ToString());
                        item.SubItems.Add(reader[4].ToString());
                        item.SubItems.Add(reader[5].ToString());
                        listView3.Items.Add(item);
                    }
                    reader.Close();
                }
                else
                {
                    ErrMsg = "";
                reader = o.OrvosLista1(ref ErrMsg);
                listView3.Items.Clear();

                if (ErrMsg == "OK")
                {
                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem(reader[0].ToString());
                        item.SubItems.Add(reader[1].ToString());
                        item.SubItems.Add(reader[2].ToString());
                        item.SubItems.Add(reader[3].ToString());
                        item.SubItems.Add(reader[4].ToString());
                        item.SubItems.Add(reader[5].ToString());
                        listView3.Items.Add(item);
                    }
                    reader.Close();
                }
                }
            }
        }

        private void txtFilterBetegNev_TextChanged(object sender, EventArgs e)
        {
            if (txtFilterBetegNev.Text != "")
            {
                listView1.Items.Clear();
                string ErrMsg = "";
                String fL = txtFilterBetegNev.Text[0].ToString();
                SqlDataReader reader = b.ElsoBetegLista(ref ErrMsg, fL);
                if (ErrMsg == "OK")
                {
                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem(reader[0].ToString());
                        item.SubItems.Add(reader[1].ToString());
                        item.SubItems.Add(reader[2].ToString());
                        item.SubItems.Add(reader[3].ToString());
                        item.SubItems.Add(reader[4].ToString());
                        listView1.Items.Add(item);
                    }
                    reader.Close();
                }
                else
                {
                    ErrMsg = "";
                    reader = b.BetegLista(ref ErrMsg);
                    listView1.Items.Clear();
                    if (ErrMsg == "OK")
                    {
                        while (reader.Read())
                        {
                            ListViewItem item = new ListViewItem(reader[0].ToString());
                            item.SubItems.Add(reader[1].ToString());
                            item.SubItems.Add(reader[2].ToString());
                            item.SubItems.Add(reader[3].ToString());
                            item.SubItems.Add(reader[4].ToString());
                            listView1.Items.Add(item);
                        }
                        reader.Close();
                    }
                }
            }
        }

        private void txtFilterBetegID_TextChanged(object sender, EventArgs e)
        {
            if (txtFilterBetegID.Text != "")
            {
                listView2.Items.Clear();
                string ErrMsg = "";
                String fL = txtFilterBetegID.Text[0].ToString();
                SqlDataReader reader = elo.ElsoBetegID(ref ErrMsg, fL);
                if (ErrMsg == "OK")
                {
                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem(reader[0].ToString());
                        item.SubItems.Add(reader[1].ToString());
                        item.SubItems.Add(reader[2].ToString());
                        item.SubItems.Add(reader[3].ToString());
                        item.SubItems.Add(reader[4].ToString());
                        item.SubItems.Add(reader[5].ToString());
                        item.SubItems.Add(reader[6].ToString());
                        listView2.Items.Add(item);
                    }
                    reader.Close();
                }
                else
                {
                    ErrMsg = "";
                    reader = elo.ElojegyzesLista(ref ErrMsg);
                    listView2.Items.Clear();

                    if (ErrMsg == "OK")
                    {
                        while (reader.Read())
                        {
                            ListViewItem item = new ListViewItem(reader[0].ToString());
                            item.SubItems.Add(reader[1].ToString());
                            item.SubItems.Add(reader[2].ToString());
                            item.SubItems.Add(reader[3].ToString());
                            item.SubItems.Add(reader[4].ToString());
                            item.SubItems.Add(reader[5].ToString());
                            item.SubItems.Add(reader[6].ToString());
                            listView2.Items.Add(item);
                        }
                        reader.Close();
                    }
                }
            }
        }

        private void txtFilterSzak_TextChanged(object sender, EventArgs e)
        {
            if (txtFilterSzak.Text != "")
            {
                listView2.Items.Clear();
                string ErrMsg = "";
                String fL = txtFilterSzak.Text[0].ToString();
                SqlDataReader reader = o.ElsoOrvosSzak(ref ErrMsg, fL);
                if (ErrMsg == "OK")
                {
                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem(reader[0].ToString());
                        item.SubItems.Add(reader[1].ToString());
                        item.SubItems.Add(reader[2].ToString());
                        item.SubItems.Add(reader[3].ToString());
                        item.SubItems.Add(reader[4].ToString());
                        item.SubItems.Add(reader[5].ToString());
                        listView3.Items.Add(item);
                    }
                    reader.Close();
                }
                else
                {
                    ErrMsg = "";
                    reader = o.OrvosLista1(ref ErrMsg);
                    listView3.Items.Clear();

                    if (ErrMsg == "OK")
                    {
                        while (reader.Read())
                        {
                            ListViewItem item = new ListViewItem(reader[0].ToString());
                            item.SubItems.Add(reader[1].ToString());
                            item.SubItems.Add(reader[2].ToString());
                            item.SubItems.Add(reader[3].ToString());
                            item.SubItems.Add(reader[4].ToString());
                            item.SubItems.Add(reader[5].ToString());
                            listView3.Items.Add(item);
                        }
                        reader.Close();
                    }
                }
            }
        }

        private void butMent_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                elo.InsertElojegyzes(txtEloID.Text, dateDatum.Text, dateOra.Text, comboBetegID.Text, comboOrvosID.Text, comboAsszID.Text, comboMuto.Text);
            }
            else
            {
                elo.UpdateElojegyzes(txtEloID.Text, dateDatum.Text, dateOra.Text, comboBetegID.Text, comboOrvosID.Text, comboAsszID.Text, comboMuto.Text);
            }
            this.Betoltes2();
        }

     

        
        
    }
}
